<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CateController extends Controller
{   
    /*
    * 分类添加
    */
    public function  getAdd()
    {
        //读取信息
        $cates = DB::table('cates')->get();
        return view('admin.cate.add',['cates'=>$cates]);
    }

    public function postInsert(Request $request)
    {   
        // dd($request->all());
        //进行表单验证
        $this->validate($request,[
            'name' => 'required|unique:cates,name',
        ],[
            'name.required' => '分类名称不能为空', 
            'name.unique' => '分类名称已存在'
        ]);
        //获取数据
        $data = $request -> except(['_token']);
        // dd($data);
        //拼接path
        //判断是否为顶级分类
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            //如果不是顶级分类
            //读取父级分类的信息
            $p = DB::table('cates')->where('id',$data['pid'])->first();
            $data['path'] = $p->path.','.$p->id;
        }
        // dd($data);
        $res = DB::table('cates')->insert($data);
        //提醒
        if($res) {
            return redirect('/cate/index')->with('info','添加成功!!!');
        }else{
            return redirect('/cate/index')->with('info','添加失败!!!');
        }
    }  

    /*
    * 分类列表显示
    */   
    public function getIndex(Request $request)
    {
        //读取内容
        $res = self::getCateInfo();
        //分配
        return view('admin.cate.index',['cates'=>$res,'request'=>$request]);
    }

    /*
    * 分类的修改
    */
    public function getEdit(Request $request)
    {
        //读取当前的信息
        $res = DB::table('cates')->where('id',$request->input('id'))->first();
        //读取所有的分类
        $cates = DB::table('cates')->get();
        //显示
        return view('admin.cate.edit',['cates'=>$cates,'info'=>$res]);
    }

    /*
    * 分类的更新
    */
    public function postUpdate(Request $request)
    {
        //获取数据
        $data = $request->except(['_token']);
        //
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            //如果不是顶级分类
            //读取父级分类的信息
            $p = DB::table('cates')->where('id',$data['pid'])->first();
            $data['path'] = $p->path.','.$p->id;
        }
        // dd($data);
        $res = DB::table('cates')->where('id',$data['id'])->update($data);
        //提醒
        if($res){
            return redirect('/cate/index')->with('info','修改成功!!!');
        }else{
            return redirect('/cate/index')->with('info','修改失败!!!');
        }
    }

    /*
    * 分类的删除
    */
    public function getDelete(Request $request)
    {
        //读取当前id
        $id = $request -> input('id');
        if(empty($id)) abort(404);
        //读取信息
        $info = DB::table('cates')->where('id',$id)->first();
        //拼接路径
        $path = $info->path.','.$info->id;
        //删除
        $res = DB::table('cates')->where('id',$id)->delete();
        $res2 = DB::table('cates')->where('path','like' ,$path.'%')->delete();
        //
        if($res){
            return redirect('/cate/index')->with('info','删除成功!!!');
        }else{
            return redirect('/cate/index')->with('info','删除失败!!!');
        }
    }

    /*
    * 获取分类信息
    */
     public static function getCateInfo()
    {
        $res = DB::table('cates')->select(db::raw('*, concat(path,",",id) as paths'))->orderBy('paths')->get();
        foreach($res as $k=>$v){
            $total = count(explode(',',$v->path));
            $v->name = str_repeat('|----',$total-1).$v->name;
        }
        return $res;
    }  

}
